home *** CD-ROM | disk | FTP | other *** search
- package com.arpitonline.controls
- {
- import flash.events.Event;
- import flash.events.MouseEvent;
- import mx.controls.Image;
- import mx.core.ScrollControlBase;
- import mx.core.ScrollPolicy;
- import mx.events.FlexEvent;
- import mx.events.ScrollEvent;
- import mx.events.ScrollEventDirection;
-
- public class ScrollImage extends ScrollControlBase
- {
- protected var _source:String;
-
- private var oldMouseY:Number = NaN;
-
- private var oldMouseX:Number = NaN;
-
- protected var lineScrollSize:int = 10;
-
- private var deltaX:Number;
-
- private var deltaY:Number;
-
- protected var img:Image;
-
- public function ScrollImage()
- {
- lineScrollSize = 10;
- oldMouseX = NaN;
- oldMouseY = NaN;
- super();
- this.horizontalScrollPolicy = ScrollPolicy.AUTO;
- }
-
- public function get source() : String
- {
- return _source;
- }
-
- override protected function mouseWheelHandler(param1:MouseEvent) : void
- {
- if(param1.delta > 1)
- {
- if(img.y + img.height >= this.height)
- {
- return;
- }
- }
- else if(img.y <= 0)
- {
- return;
- }
- super.mouseWheelHandler(param1);
- }
-
- private function onUpdateComplete(param1:Event) : void
- {
- if(!img.content || img.content.width == 0 || img.content.height == 0)
- {
- return;
- }
- img.removeEventListener(FlexEvent.UPDATE_COMPLETE,onUpdateComplete);
- img.width = img.content.width;
- img.height = img.content.height;
- this.setScrollBarProperties(img.width,unscaledWidth,img.height,unscaledHeight);
- if(null != this.horizontalScrollBar)
- {
- this.horizontalScrollBar.lineScrollSize = lineScrollSize;
- }
- if(null != this.verticalScrollBar)
- {
- this.verticalScrollBar.lineScrollSize = lineScrollSize;
- }
- zero();
- }
-
- public function set source(param1:String) : void
- {
- _source = param1;
- invalidateProperties();
- }
-
- private function onScroll(param1:ScrollEvent) : void
- {
- if(param1.direction == ScrollEventDirection.VERTICAL)
- {
- this.img.y = -param1.position;
- if(this.img.y + this.img.height < this.height)
- {
- img.y += this.height - (img.y + img.height);
- }
- if(img.y > 0)
- {
- img.y = 0;
- }
- }
- else
- {
- this.img.x = -param1.position;
- if(img.x + img.width < this.width)
- {
- img.x += this.width - (img.x + img.width);
- }
- if(img.x > 0)
- {
- img.x = 0;
- }
- }
- }
-
- override protected function commitProperties() : void
- {
- super.commitProperties();
- if(!_source || _source == img.source)
- {
- return;
- }
- img.source = _source;
- img.addEventListener(FlexEvent.UPDATE_COMPLETE,onUpdateComplete);
- }
-
- public function set scale(param1:Number) : void
- {
- this.img.width = img.content.width * param1;
- this.img.height = img.content.height * param1;
- this.setScrollBarProperties(img.content.width * param1,unscaledWidth,img.content.height * param1,unscaledHeight);
- if(img.x + img.width < this.width)
- {
- img.x += this.width - (img.x + img.width);
- }
- if(img.y + img.height < this.height)
- {
- img.y += this.height - (img.y + img.height);
- }
- }
-
- public function zero() : void
- {
- img.x = img.y = 0;
- img.buttonMode = true;
- this.horizontalScrollPosition = 0;
- this.verticalScrollPosition = 0;
- }
-
- override protected function createChildren() : void
- {
- super.createChildren();
- img = new Image();
- img.showBusyCursor = true;
- img.includeInLayout = false;
- addChild(img);
- img.mask = this.maskShape;
- this.addEventListener(ScrollEvent.SCROLL,onScroll);
- img.addEventListener(MouseEvent.MOUSE_DOWN,onMouseDown);
- img.addEventListener(MouseEvent.MOUSE_UP,onMouseUp);
- }
-
- private function onMouseMove(param1:MouseEvent) : void
- {
- deltaX = -(mouseX - oldMouseX);
- deltaY = -(mouseY - oldMouseY);
- this.horizontalScrollPosition += deltaX;
- this.verticalScrollPosition += deltaY;
- if(deltaX < 0 && img.x < 0)
- {
- dispatchEvent(new ScrollEvent(ScrollEvent.SCROLL,false,false,null,this.horizontalScrollPosition,"horizontal",deltaX));
- }
- else if(deltaX > 0 && img.x + img.width > this.width)
- {
- dispatchEvent(new ScrollEvent(ScrollEvent.SCROLL,false,false,null,this.horizontalScrollPosition,"horizontal",deltaY));
- }
- if(deltaY > 0 && img.y + img.height > this.height)
- {
- dispatchEvent(new ScrollEvent(ScrollEvent.SCROLL,false,false,null,this.verticalScrollPosition,"vertical",deltaY));
- }
- else if(deltaY < 0 && img.y < 0)
- {
- dispatchEvent(new ScrollEvent(ScrollEvent.SCROLL,false,false,null,this.verticalScrollPosition,"vertical",deltaY));
- }
- oldMouseX = mouseX;
- oldMouseY = mouseY;
- }
-
- private function onMouseUp(param1:MouseEvent) : void
- {
- this.removeEventListener(MouseEvent.MOUSE_MOVE,onMouseMove);
- }
-
- private function onMouseDown(param1:MouseEvent) : void
- {
- oldMouseX = this.mouseX;
- oldMouseY = this.mouseY;
- this.addEventListener(MouseEvent.MOUSE_MOVE,onMouseMove);
- stage.addEventListener(MouseEvent.MOUSE_UP,onMouseUp);
- }
- }
- }
-
-